const express = require('express');
const router = express.Router();
const AccountController = require('../controllers/AccountController');
const authenticate = require('../middleware/auth');

/**
 * @swagger
 * tags:
 *   name: Accounts
 *   description: 账户管理
 */

// All routes require authentication
router.use((req, res, next) => {
  console.log('Account route middleware:', req.path, req.method);
  next();
});

router.use(authenticate);

/**
 * @swagger
 * /api/accounts/summary:
 *   get:
 *     summary: 获取账户汇总信息
 *     tags: [Accounts]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取账户汇总信息
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/summary', (req, res, next) => {
  console.log('Account summary route called');
  next();
}, AccountController.getAccountSummary);

/**
 * @swagger
 * /api/accounts:
 *   post:
 *     summary: 创建新账户
 *     tags: [Accounts]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *             properties:
 *               name:
 *                 type: string
 *                 description: 账户名称
 *               type:
 *                 type: string
 *                 enum: [银行卡, 现金, 信用卡, 投资账户, 其他]
 *                 description: 账户类型
 *               balance:
 *                 type: number
 *                 format: float
 *                 description: 初始余额
 *               currency:
 *                 type: string
 *                 description: 货币代码
 *     responses:
 *       201:
 *         description: 账户创建成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.post('/', AccountController.createAccount);

/**
 * @swagger
 * /api/accounts:
 *   get:
 *     summary: 获取用户账户列表
 *     tags: [Accounts]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取账户列表
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/', AccountController.getAccounts);

/**
 * @swagger
 * /api/accounts/{id}:
 *   get:
 *     summary: 获取特定账户详情
 *     tags: [Accounts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 账户ID
 *     responses:
 *       200:
 *         description: 成功获取账户详情
 *       401:
 *         description: 未授权
 *       404:
 *         description: 账户未找到
 *       500:
 *         description: 服务器内部错误
 */
router.get('/:id', AccountController.getAccountById);

/**
 * @swagger
 * /api/accounts/{id}:
 *   put:
 *     summary: 更新账户
 *     tags: [Accounts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 账户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 账户名称
 *               type:
 *                 type: string
 *                 enum: [银行卡, 现金, 信用卡, 投资账户, 其他]
 *                 description: 账户类型
 *               balance:
 *                 type: number
 *                 format: float
 *                 description: 余额
 *               currency:
 *                 type: string
 *                 description: 货币代码
 *               is_active:
 *                 type: boolean
 *                 description: 是否激活
 *     responses:
 *       200:
 *         description: 账户更新成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 账户未找到
 *       500:
 *         description: 服务器内部错误
 */
router.put('/:id', AccountController.updateAccount);

/**
 * @swagger
 * /api/accounts/{id}:
 *   delete:
 *     summary: 删除账户
 *     tags: [Accounts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 账户ID
 *     responses:
 *       200:
 *         description: 账户删除成功
 *       400:
 *         description: 无法删除有关联账单的账户
 *       401:
 *         description: 未授权
 *       404:
 *         description: 账户未找到
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/:id', AccountController.deleteAccount);

module.exports = router;